
import { _decorator, Component, Node, SpriteFrame, Asset, Texture2D, Sprite, Label } from 'cc';
import Loader from '../../Script/Common/Loader';
import PlayerData from '../../Script/Common/PlayerData';
import yyComponent from '../../Script/Common/yyComponent';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item extends yyComponent {

    goodsInfo = null;
    goodsUseType = null;

    init(data?) {
        if (!!data) {
            this.setData(data);
        }
        this.node.on(Node.EventType.TOUCH_END, this.onClickItem, this);
    }
    reuse(data?) {
        if (!!data) {
            this.setData(data);
        }
    }
    setData(data) {
        if (!data) {
            console.log("数据不存在");
            return;
        }
        this.goodsInfo = data;
        this.loadImage(data["goodsType"] + data["id"], this.setImage);
    }
    unuse() {

    }
    loadImage(name: string, cb?: Function) {
        Loader.loadBundle("UI", () => {
            Loader.loadBundleRes("UI", "Image/shop/skin/" + name + "/spriteFrame", (res) => {
                if (!!cb) {
                    cb.call(this, res);
                }
            });
        }, false, false);
    }
    @property(Sprite)
    goodsImg: Sprite = null;
    setImage(img) {
        if (!!img) {
            this.goodsImg.spriteFrame = img;
        }
        this.setInfo();
    }
    @property(Label)
    goldLabel: Label = null;
    @property(Label)
    videoLabel: Label = null;
    setInfo() {
        this.setGoldNum();

        let playerData = PlayerData.getData("gameData");
        let type = this.goodsInfo["goodsType"];
        if (type == GlobalEnum.GoodsType.skin) {
            let count = playerData["PlayerSkin"]["videoCount"][String(this.goodsInfo["id"])];
            if (!count) {
                count = 0;
            };
            // this.videoLabel.string = count + "/" + this.goodsInfo["videoValue"];
            let owned = playerData["PlayerSkin"]["owned"];
            let index = owned.indexOf(this.goodsInfo["id"]);
            if (index >= 0) {
                this.goodsUseType = "owned";
                if (playerData["PlayerSkin"]["cur"] == this.goodsInfo["id"]) {
                    this.goodsUseType = "using";
                }
            } else {
                this.goodsUseType = "none";
            }
        } else if (type == GlobalEnum.GoodsType.hat) {
            let count = playerData["HatSkin"]["videoCount"][String(this.goodsInfo["id"])];
            if (!count) {
                count = 0;
            };
            // this.videoLabel.string = count + "/" + this.goodsInfo["videoValue"];

            let owned = playerData["HatSkin"]["owned"];
            let index = owned.indexOf(this.goodsInfo["id"]);
            if (index >= 0) {
                this.goodsUseType = "owned";
                if (playerData["HatSkin"]["cur"] == this.goodsInfo["id"]) {
                    this.goodsUseType = "using";
                }
            } else {
                this.goodsUseType = "none";
            }
        } else {
            let count = playerData["WeaponSkin"]["videoCount"][this.goodsInfo["goodsType"]][String(this.goodsInfo["id"])];
            if (!count) {
                count = 0;
            };
            // this.videoLabel.string = count + "/" + this.goodsInfo["videoValue"];
            let owned = playerData["WeaponSkin"]["owned"];
            let index = owned[this.goodsInfo["goodsType"]].indexOf(this.goodsInfo["id"]);
            if (index >= 0) {
                this.goodsUseType = "owned";
                if (playerData["WeaponSkin"]["cur"]["type"] == this.goodsInfo["goodsType"] && playerData["WeaponSkin"]["cur"]["id"] == this.goodsInfo["id"]) {
                    this.goodsUseType = "using";
                }
            } else {
                this.goodsUseType = "none";
            }
        }
        this.updateBtn(this.goodsUseType);

    }

    setUseType() {
        let dataPath = "gameData";
        let curPath = ".cur";
        let curPath2 = ".cur";

        let type = this.goodsInfo["goodsType"];
        if (type == GlobalEnum.GoodsType.skin) {
            dataPath += ".PlayerSkin";
        } else if (type == GlobalEnum.GoodsType.hat) {
            dataPath += ".HatSkin";
        } else {
            dataPath += ".WeaponSkin";
            curPath += ".id";
            curPath2 += ".type";
        }
        let curId = PlayerData.getData(dataPath + curPath);
        if (curId == this.goodsInfo["id"]) {
            let type = PlayerData.getData(dataPath + curPath2);
            if (!type || type == this.goodsInfo["goodsType"]) {
                this.goodsUseType = "using";
                return;
            }
        }


    }
    setGoldNum() {
        this.goldLabel.string = this.goodsInfo["costValue"];
    }


    @property(Node)
    btnBuy: Node = null;
    @property(Node)
    btnVideo: Node = null;
    @property(Node)
    btnState: Node = null;
    updateBtn(type) {
        if (type == "owned") {
            this.btnBuy.active = false;
            this.btnVideo.active = false;
            this.btnState.active = true;
            this.setBtnState("owned");
            this.unuseBg();
        } else if (type == "using") {
            this.btnBuy.active = false;
            this.btnVideo.active = false;
            this.btnState.active = true;
            this.setBtnState("using");
            this.useBg();
        } else if (type == "none") {
            if (this.goodsInfo["costType"] == "spin") {
                this.btnBuy.active = false;
                this.btnVideo.active = false;
                this.btnState.active = true;
                this.setBtnState("none");
            } else {
                this.btnBuy.active = true;
                this.btnVideo.active = true;
                this.btnState.active = false;
            }
            this.unuseBg();
        }
    }

    @property(SpriteFrame)
    stateImg: SpriteFrame[] = [];
    setBtnState(type) {
        if (type == "owned") {
            // this.stateLab.string = "使用";
            this.btnState.getComponent(Sprite).spriteFrame = this.stateImg[0];
        } else if (type == "using") {
            // this.stateLab.string = "卸下";
            this.btnState.getComponent(Sprite).spriteFrame = this.stateImg[1];
        } else if (type == "none") {
            // this.stateLab.string = "抽奖";
            this.btnState.getComponent(Sprite).spriteFrame = this.stateImg[2];
        }
    }

    onBtnBuy() {
        this.playClickBtn();
        let ownedGold = PlayerData.getData("gameData.asset.gold");
        let needGold = this.goodsInfo["costValue"];
        if (needGold > ownedGold) {
            this.showMsg("金币不够~");
            this.showUI(GlobalEnum.UI.getGoldByVideo);
        } else {
            PlayerData.subGold(needGold);
            this.getGoods();
            this.setInfo();
        }
    }
    onBtnVideo() {
        this.playClickBtn();
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: () => {
                let path = "gameData.";
                let type = this.goodsInfo["goodsType"];
                if (type == "skin") {
                    path += "PlayerSkin.videoCount." + this.goodsInfo["id"];
                } else if (type == "hat") {
                    path += "HatSkin.videoCount." + this.goodsInfo["id"];
                } else {
                    path += "WeaponSkin.videoCount." + this.goodsInfo["goodsType"] + "." + this.goodsInfo["id"];
                }
                this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                    attribute: path,
                    value: 1,
                    mode: "+",
                    emit: true,
                    save: true,
                });
                let count = PlayerData.getData(path);
                if (count >= this.goodsInfo["videoValue"]) {
                    this.getGoods();
                }
                this.setInfo();
            },
            fail: () => {
                this.showMsg("广告拉取失败，请稍后再试~");
            },
        });
    }
    onBtnState() {
        this.playClickBtn();
        if (this.goodsUseType == "owned") {
            this.useGoods();
            this.setInfo();
            this.emit(EventType.LevelEvent.goodsUSE);
        } else if (this.goodsUseType == "using") {
            this.unuseGoods();
            this.setInfo();
            this.emit(EventType.LevelEvent.goodsUSE);
        } else if (this.goodsUseType == "none") {
            // this.showMsg("抽奖啦");
            this.hideUI(GlobalEnum.UI.shop);
            this.showUI(GlobalEnum.UI.spin);
            this.emit(EventType.LevelEvent.changeGoldPos, false);
        }
    }
    /**获取本商品 */
    getGoods() {
        let path = "gameData.";
        let type = this.goodsInfo["goodsType"];
        if (type == "skin") {
            path += "PlayerSkin.owned";
        } else if (type == "hat") {
            path += "HatSkin.owned";
        } else {
            path += "WeaponSkin.owned." + this.goodsInfo["goodsType"];
        }
        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: path,
            value: this.goodsInfo["id"],
            mode: "push",
            emit: true,
            save: true,
        });
    }
    /**使用本商品 */
    useGoods() {
        let path = "gameData.";
        let type = this.goodsInfo["goodsType"];
        let data = {};
        if (type == "skin") {
            path += "PlayerSkin.cur";
            data = this.goodsInfo["id"];
        } else if (type == "hat") {
            path += "HatSkin.cur";
            data = this.goodsInfo["id"];
        } else {
            path += "WeaponSkin.cur";
            data = { id: this.goodsInfo["id"], type: this.goodsInfo["goodsType"] };
        }
        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: path,
            value: data,
            mode: "=",
            emit: true,
            save: true,
        });
        this.preview();
    }
    /**卸载本商品 */
    unuseGoods() {
        let path = "gameData.";
        let type = this.goodsInfo["goodsType"];
        let data = {};
        if (type == "skin") {
            path += "PlayerSkin.cur";
            data = 0;
        } else if (type == "hat") {
            path += "HatSkin.cur";
            data = 0;
        } else {
            path += "WeaponSkin.cur";
            data = { id: 1, type: GlobalEnum.WeaponType.katana };
        }
        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: path,
            value: data,
            mode: "=",
            emit: true,
            save: true,
        });
        this.emit(EventType.LevelEvent.setPreview, {
            type: type,
            data: {
                id: 0,
                type: GlobalEnum.WeaponType.none,
            },
        });
    }
    preview() {
        this.emit(EventType.LevelEvent.setPreview, {
            type: this.goodsInfo["goodsType"],
            data: {
                id: this.goodsInfo["id"],
                type: this.goodsInfo["goodsType"],
            },
        });
    }
    @property(SpriteFrame)
    onBgImg: SpriteFrame = null;
    @property(SpriteFrame)
    offBgImg: SpriteFrame = null;
    @property(Sprite)
    bgSpr: Sprite = null;
    useBg() {
        this.bgSpr.spriteFrame = this.onBgImg;
    }
    unuseBg() {
        this.bgSpr.spriteFrame = this.offBgImg;
    }
    refresh() {
        this.setInfo();
    }

    onClickItem() {
        this.playClickBtn();
        // this.useBg();
        this.preview();
    }
}
